import { _decorator, Vec3, Node, Component } from "cc";
import { Orientation } from "db://assets/Scripts/Enums/Events/ActionsEnum";
import { MovingCommand } from "db://assets/Scripts/Enums/Events/ActionsEnum";
import { PlayerDataInterFace } from "db://assets/Scripts/Entity/PlayerDataInterFace";
const { ccclass, property } = _decorator;

@ccclass("PlayerMovingAction")
export class PlayerMovingAction extends Component {
  moveByCommand(dataObj: PlayerDataInterFace) {
    dataObj.coordinate = dataObj.targetNode.getPosition();

    const leftBorder = -120;
    const rightBorder = 121;
    const topBorder = 289;
    const bottomBorder = -49;

    if (dataObj.command === MovingCommand.FORWARD_MOVE) {
      if (
        dataObj.presentDirection === Orientation.TOP ||
        dataObj.presentDirection === Orientation.DOWN
      ) {
        if (
          dataObj.coordinate.y <= topBorder 
        ) {
          dataObj.coordinate.y += dataObj.speed;
        }
      }
    } else if (dataObj.command === MovingCommand.BACKWARD_MOVE) {
      if (
        dataObj.presentDirection === Orientation.TOP ||
        dataObj.presentDirection === Orientation.DOWN
      ) {
        if (
          dataObj.coordinate.y >= bottomBorder
        ) {
          dataObj.coordinate.y -= dataObj.speed;
        }
      }
    } else if (dataObj.command === MovingCommand.LEFT_MOVE) {
      if (
        dataObj.presentDirection === Orientation.LEFT ||
        dataObj.presentDirection === Orientation.RIGHT
      ) {
        if (
          dataObj.coordinate.x >= leftBorder 
        ) {
          dataObj.coordinate.x -= dataObj.speed;
        }
      }
    } else if (dataObj.command === MovingCommand.RIGHT_MOVE) {
      if (
        dataObj.presentDirection === Orientation.LEFT ||
        dataObj.presentDirection === Orientation.RIGHT
      ) {
        if (
          dataObj.coordinate.x <= rightBorder
        ) {
          dataObj.coordinate.x += dataObj.speed;
        }
      }
    }
    console.log("X,Y,z:", dataObj.coordinate);
    dataObj.targetNode.setPosition(dataObj.coordinate);
  }

  start() {
    this._name = "PlayerMovingAction";
  }
}
